$(function(){
	function getTableSource(){
		var tableSource = new $.jqx.dataAdapter({
			dataType: "json",
			dataFields:[
			    { name: "id", type: "int"},
			    { name: "uuid", type: "string" },
			    { name: "name", type: "string" },
			    { name: "isBoutique", type: "boolean"}
			],
			id: "id",
			url: "groupBuy/getAllGoods.do"
		});
		return tableSource;
	}
	
	$("#dataTable").jqxDataTable({
		width: "100%",
        source: getTableSource(),
        pageable: true,
        serverProcessing: true,
        pageSize: 15,
        pagerButtonsCount: 10,
        columnsResize: true,
        sortable: true,
        columns: [
            { text: "序号", width:"15%", cellsRenderer: function(row) { return (row + 1).toString(); } },
            { text: "商品名称", dataField : "name" },
            { text: "是否推荐", cellsRenderer: function(row, column, value, rowData){
            	console.log(rowData.isBoutique);
            	if(rowData.isBoutique === false){
            		return "<input type='button' onclick=\"isBoutique('"+ rowData.uuid +"', 'true')\" class='btn-sm btn-primary' value='设置推荐' />";
            	}else{
                	return "<input type='button' onclick=\"isBoutique('"+ rowData.uuid +"', 'false')\" class='btn-sm btn-warning' value='取消推荐' />";
            	}
            }}
        ],
	});
});

function isBoutique(uuid,type){
	$.ajax({
		url:"groupBuy/isBoutique.do",
		data:{"uuid":uuid,"isBoutique":type},
		type:"post",
		success:function(result){
			console.log(result);
			layer.msg(result.msg);
			update($("#dataTable"));
		}
	});
}

/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}